<?php

namespace app\index\controller;

use think\Controller;

class Withdraw extends Base {
	public function index() {
		if ($this->request->isPost ()) {
			$paypwd = $this->request->param ( 'paypwd' );
			$money = $this->request->param ( 'money' );
			if ($this->member ['freeze'] == 1)
				echo json_encode ( array ('code' => 0,'msg' => lang ( 'freeze_msg' ) ) );
			elseif ($money < 5 || $money > 1000000) {
				echo json_encode ( array ('code' => 0,'msg' => lang ( 'withdrawal_amount_error' ) ) );
			} elseif ($paypwd != $this->member ['paypwd'])
				echo json_encode ( array ('code' => 0,'msg' => lang ( 'paypwd_error' ) ) );
			elseif ($money > $this->member ['money']) {
				echo json_encode ( array ('code' => 0,'msg' => lang ( 'insufficient_balance' ) ) );
			} else {
				$data ['user_id'] = $this->uid;
				$data ['money'] = $money;
				$data ['paypwd'] = $this->request->param ( 'paypwd' );
				$data ['create_time'] = time ();
				$data ['status'] = 0;
				$data ['wallet_type'] = $this->request->param ( 'wallet_type' );
				$data ['usdt_wallet'] = $this->request->param ( 'usdt_wallet' );
				$log = model ( 'Withdraw' )->add ( $data );
				if ($log > 0) {
					model ( 'Member' )->incLockMoney ( array ([ 'user_id','=',$this->uid ] ), $money );
					model ( 'Member' )->decMoney ( array ([ 'user_id','=',$this->uid ] ), $money );
					unset ( $data );
					$data ['wallet_type'] = $this->request->param ( 'wallet_type' );
					$data ['usdt_wallet'] = $this->request->param ( 'usdt_wallet' );
					model ( 'Member' )->edit ( array ([ 'user_id','=',$this->uid ] ), $data );

                    //设置提现提醒
                    $notice = cache('order_notice_config');
                    if (!empty($notice)){
                        if($notice['order_notice_status'] == 1){
                            //设置提现提醒
                            cache('new_withdraw',1,['expire' => $notice['ajax_interval']]);
                        }
                    }

					echo json_encode ( array ('code' => 99,'msg' => lang ( 'success' ) ) );
				} else
					echo json_encode ( array ('code' => 0,'msg' => lang ( 'fail' ) ) );
			}
		} else {
			$wallet = model ( 'Wallet' )->getAll ( array ([ 'user_id','=',$this->uid ] ) );
			//if (count ( $wallet ) == 0)
			//	$this->redirect ( url ( 'Wallet/index' ) );
			$data = model ( 'Withdraw' )->getlist ( array ([ 'w.user_id','=',$this->uid ] ) );
			$this->assign ( 'last', $data->items () [0] );
			$this->assign ( 'data', $data );
			return $this->fetch ();
		}
	}
	function recode() {
		$data = model ( 'Withdraw' )->getlist ( array ([ 'w.user_id','=',$this->uid ] ) );
		$this->assign ( 'last', $data->items () [0] );
		$this->assign ( 'data', $data );
		return $this->fetch ();
	}
	function getQr() {
		$type = $this->request->param ( 'type' );
		switch ($type) {
			case 'trc20' :
				$info = model ( 'Wallet' )->getInfo ( array ([ 'user_id','=',$this->uid ],[ 'type','=','trc20' ] ) );
				break;
			case 'erc20' :
				$info = model ( 'Wallet' )->getInfo ( array ([ 'user_id','=',$this->uid ],[ 'type','=','erc20' ] ) );
				break;
		}
		echo json_encode ( array ('key' => $info ['address'] ) );
	}
}
